import 'package:get/get.dart';
import 'package:repair_pda/model/response_result.dart';
import 'package:repair_pda/model/login_model.dart';
import 'package:repair_pda/service/api_service.dart';
import 'package:repair_pda/utils/navigator_utils.dart';

class LoginController extends GetxController {
  final ApiService apiService = Get.find();
  var isLoading = false.obs;
  var userToken = ''.obs;
  // var loginResult = ResponseResult<LoginModel>.empty().obs;
  var loginResult = ResponseResult<String>.empty().obs;
  LoginModel loginModel = LoginModel.empty();

  Future<void> performLogin(String username, String password, String flights, String team) async {
    try {
      isLoading(true);

      // 创建登录模型，进入系统后会用到其中的属性
      loginModel = LoginModel(
        username: username,
        password: password,
        flights: flights,// 班次
        team: team,      // 班组
      );

      // 调用API
      //final result = await apiService.login(loginModel);
      final result = getLoginData();

      // 更新结果
      loginResult.value = result;

      // 根据结果处理业务逻辑
      if (result.success == true || result.code == 1) {
        setToken(result.data ?? '');
        print('登录成功');
        // NavigatorUtils.goHome();
        NavigatorUtils.goTabNavigator();
      } else {
        // 登录失败，显示错误信息
        //Get.snackbar('错误', result.msg ?? '登录失败');
        print('登录失败：${result.msg}');
      }
    } finally {
      isLoading(false);
    }
  }

  void setToken(String token){
    userToken.value = token;
  }

  ResponseResult<String> getLoginData(){
    // final LoginModel loginModel = LoginModel(username: 'chanchaw',password: '123456',flights: 'A',team: '中');
    // final ResponseResult<LoginModel> ret = ResponseResult(success: true,data:loginModel,msg:'',code:0);
    final ResponseResult<String> ret = ResponseResult(success: true,data:'token:sdfasdfasdfasdf',msg:'',code:0);
    return ret;
  }
}